#!/bin/bash

APP=gmall

# 如果是输入的日期按照取输入日期；如果没输入日期取当前时间的前一天
if [ -n "$2" ] ;then
    do_date=$2
else
    do_date=`date -d "-1 day" +%F`
fi


# ===========================================================================================
#                                 定义变量：各个表数据加载语句
# ===========================================================================================

# 订单信息表
ods_order_info="
load data inpath '/origin_data/${APP}/db/order_info/${do_date}' OVERWRITE into table ${APP}.ods_order_info partition(dt='${do_date}');"

# 订单明细表
ods_order_detail="
load data inpath '/origin_data/${APP}/db/order_detail/${do_date}' OVERWRITE into table ${APP}.ods_order_detail partition(dt='${do_date}');"

# sku商品信息表
ods_sku_info="
load data inpath '/origin_data/${APP}/db/sku_info/${do_date}' OVERWRITE into table ${APP}.ods_sku_info partition(dt='${do_date}');"

# 用户信息表
ods_user_info="
load data inpath '/origin_data/${APP}/db/user_info/${do_date}' OVERWRITE into table ${APP}.ods_user_info partition(dt='${do_date}');"

# 支付信息表
ods_payment_info="
load data inpath '/origin_data/${APP}/db/payment_info/${do_date}' OVERWRITE into table ${APP}.ods_payment_info partition(dt='${do_date}');"

# 一级品类表
ods_base_category1="
load data inpath '/origin_data/${APP}/db/base_category1/${do_date}' OVERWRITE into table ${APP}.ods_base_category1 partition(dt='${do_date}');"

# 二级品类表
ods_base_category2="
load data inpath '/origin_data/${APP}/db/base_category2/${do_date}' OVERWRITE into table ${APP}.ods_base_category2 partition(dt='${do_date}');"

# 三级品类表
ods_base_category3="
load data inpath '/origin_data/${APP}/db/base_category3/${do_date}' OVERWRITE into table ${APP}.ods_base_category3 partition(dt='${do_date}'); "

# 品牌表
ods_base_trademark="
load data inpath '/origin_data/${APP}/db/base_trademark/${do_date}' OVERWRITE into table ${APP}.ods_base_trademark partition(dt='${do_date}'); "

# 活动信息表
ods_activity_info="
load data inpath '/origin_data/${APP}/db/activity_info/${do_date}' OVERWRITE into table ${APP}.ods_activity_info partition(dt='${do_date}'); "

# 加购信息表
ods_cart_info="
load data inpath '/origin_data/${APP}/db/cart_info/${do_date}' OVERWRITE into table ${APP}.ods_cart_info partition(dt='${do_date}'); "

# 评论信息表
ods_comment_info="
load data inpath '/origin_data/${APP}/db/comment_info/${do_date}' OVERWRITE into table ${APP}.ods_comment_info partition(dt='${do_date}'); "

# 优惠卷信息表
ods_coupon_info="
load data inpath '/origin_data/${APP}/db/coupon_info/${do_date}' OVERWRITE into table ${APP}.ods_coupon_info partition(dt='${do_date}'); "

# 优惠卷使用表
ods_coupon_use="
load data inpath '/origin_data/${APP}/db/coupon_use/${do_date}' OVERWRITE into table ${APP}.ods_coupon_use partition(dt='${do_date}'); "

# 收藏信息表
ods_favor_info="
load data inpath '/origin_data/${APP}/db/favor_info/${do_date}' OVERWRITE into table ${APP}.ods_favor_info partition(dt='${do_date}'); "

# 退单信息表
ods_order_refund_info="
load data inpath '/origin_data/${APP}/db/order_refund_info/${do_date}' OVERWRITE into table ${APP}.ods_order_refund_info partition(dt='${do_date}'); "

# 订单状态日志表
ods_order_status_log="
load data inpath '/origin_data/${APP}/db/order_status_log/${do_date}' OVERWRITE into table ${APP}.ods_order_status_log partition(dt='${do_date}'); "

# spu商品信息表
ods_spu_info="
load data inpath '/origin_data/${APP}/db/spu_info/${do_date}' OVERWRITE into table ${APP}.ods_spu_info partition(dt='${do_date}'); "

# 活动规则表
ods_activity_rule="
load data inpath '/origin_data/${APP}/db/activity_rule/${do_date}' OVERWRITE into table ${APP}.ods_activity_rule partition(dt='${do_date}');"

# 字典表
ods_base_dic="
load data inpath '/origin_data/${APP}/db/base_dic/${do_date}' OVERWRITE into table ${APP}.ods_base_dic partition(dt='${do_date}'); "

# 订单明细关联活动表
ods_order_detail_activity="
load data inpath '/origin_data/${APP}/db/order_detail_activity/${do_date}' OVERWRITE into table ${APP}.ods_order_detail_activity partition(dt='${do_date}'); "

# 订单明细关联优惠卷表
ods_order_detail_coupon="
load data inpath '/origin_data/${APP}/db/order_detail_coupon/${do_date}' OVERWRITE into table ${APP}.ods_order_detail_coupon partition(dt='${do_date}'); "

# 退款表
ods_refund_payment="
load data inpath '/origin_data/${APP}/db/refund_payment/${do_date}' OVERWRITE into table ${APP}.ods_refund_payment partition(dt='${do_date}'); "

# sku商品属性表
ods_sku_attr_value="
load data inpath '/origin_data/${APP}/db/sku_attr_value/${do_date}' OVERWRITE into table ${APP}.ods_sku_attr_value partition(dt='${do_date}'); "

# sku商品销售属性表
ods_sku_sale_attr_value="
load data inpath '/origin_data/${APP}/db/sku_sale_attr_value/${do_date}' OVERWRITE into table ${APP}.ods_sku_sale_attr_value partition(dt='${do_date}'); "

# 省份表
ods_base_province="
load data inpath '/origin_data/${APP}/db/base_province/${do_date}' OVERWRITE into table ${APP}.ods_base_province;"

# 区域表
ods_base_region="
load data inpath '/origin_data/${APP}/db/base_region/${do_date}' OVERWRITE into table ${APP}.ods_base_region;"


case $1 in
    "ods_order_info"){
        hive -e "${ods_order_info}"
    };;
    "ods_order_detail"){
        hive -e "${ods_order_detail}"
    };;
    "ods_sku_info"){
        hive -e "${ods_sku_info}"
    };;
    "ods_user_info"){
        hive -e "${ods_user_info}"
    };;
    "ods_payment_info"){
        hive -e "${ods_payment_info}"
    };;
    "ods_base_category1"){
        hive -e "${ods_base_category1}"
    };;
    "ods_base_category2"){
        hive -e "${ods_base_category2}"
    };;
    "ods_base_category3"){
        hive -e "${ods_base_category3}"
    };;
    "ods_base_trademark"){
        hive -e "${ods_base_trademark}"
    };;
    "ods_activity_info"){
        hive -e "${ods_activity_info}"
    };;
    "ods_cart_info"){
        hive -e "${ods_cart_info}"
    };;
    "ods_comment_info"){
        hive -e "${ods_comment_info}"
    };;
    "ods_coupon_info"){
        hive -e "${ods_coupon_info}"
    };;
    "ods_coupon_use"){
        hive -e "${ods_coupon_use}"
    };;
    "ods_favor_info"){
        hive -e "${ods_favor_info}"
    };;
    "ods_order_refund_info"){
        hive -e "${ods_order_refund_info}"
    };;
    "ods_order_status_log"){
        hive -e "${ods_order_status_log}"
    };;
    "ods_spu_info"){
        hive -e "${ods_spu_info}"
    };;
    "ods_activity_rule"){
        hive -e "${ods_activity_rule}"
    };;
    "ods_base_dic"){
        hive -e "${ods_base_dic}"
    };;
    "ods_order_detail_activity"){
        hive -e "${ods_order_detail_activity}"
    };;
    "ods_order_detail_coupon"){
        hive -e "${ods_order_detail_coupon}"
    };;
    "ods_refund_payment"){
        hive -e "${ods_refund_payment}"
    };;
    "ods_sku_attr_value"){
        hive -e "${ods_sku_attr_value}"
    };;
    "ods_sku_sale_attr_value"){
        hive -e "${ods_sku_sale_attr_value}"
    };;
    "ods_base_province"){
        hive -e "${ods_base_province}"
    };;
    "ods_base_region"){
        hive -e "${ods_base_region}"
    };;
    "all"){
        hive -e "${ods_order_info}${ods_order_detail}${ods_sku_info}${ods_user_info}${ods_payment_info}${ods_base_category1}${ods_base_category2}${ods_base_category3}${ods_base_trademark}${ods_activity_info}${ods_cart_info}${ods_comment_info}${ods_coupon_info}${ods_coupon_use}${ods_favor_info}${ods_order_refund_info}${ods_order_status_log}${ods_spu_info}${ods_activity_rule}${ods_base_dic}${ods_order_detail_activity}${ods_order_detail_coupon}${ods_refund_payment}${ods_sku_attr_value}${ods_sku_sale_attr_value}${ods_base_province}${ods_base_region}"
    };;
esac


#step1. 执行权限
# chmod +x hdfs_to_ods_db.sh
#step2. 前一天数据，加载到某张表
# hdfs_to_ods_db.sh ods_order_detail
#step3. 某天数据，加载到某张表
# hdfs_to_ods_db.sh ods_order_detail 2024-01-06
#step4. 某天数据，加载所有表
# hdfs_to_ods_db.sh all 2024-01-06
#


